﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TicketApp.Web.TGIServiceProxy;
using System.Configuration;

namespace TicketApp.Web.Controllers
{
    public class EventController : Controller
    {
        //
        // GET: /Event/

        public ActionResult Index()
        {
            var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            var events = context.Events;
            return View(events);
        }

        //
        // GET: /Event/Details/5

        public ActionResult Details(int id)
        {
            var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            var evnt = context.Events.Where(e => e.Id == id).FirstOrDefault();
            return View(evnt);
        }

        //
        // GET: /Event/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Event/Create

        [HttpPost]
        public ActionResult Create(Event evnt)
        {
            var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            context.AddToEvents(evnt);
            context.SaveChanges();
            return RedirectToAction("Index");
        }

        //
        // GET: /Event/Edit/5

        public ActionResult Edit(int id)
        {
            var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            var evnt = context.Events.Where(e => e.Id == id).FirstOrDefault();
            return View(evnt);
        }

        //
        // POST: /Event/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Event evnt)
        {
            var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            context.AttachTo("Events", evnt);
            context.UpdateObject(evnt);
            context.SaveChanges();
            return RedirectToAction("Index");

        }

        //
        // GET: /Event/Delete/5

        public ActionResult Delete(int id)
        {
            var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            var evnt = context.Events.Where(e => e.Id == id).FirstOrDefault();
            return View(evnt);
        }

        //
        // POST: /Event/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, Event evnt)
        {
            var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            context.AttachTo("Events", evnt);
            context.DeleteObject(evnt);
            context.SaveChanges();
            return RedirectToAction("Index");

        }
    }
}
